﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.PlanarProcessServiceMapDomain
{
    public class PlanarProcessServiceMapService : IPlanarProcessServiceMapService
    {
        private readonly ILogUtil _logUtil;
        private readonly IPlanarProcessServiceMapInfoRepository _PlanarProcessServiceMapRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public PlanarProcessServiceMapService(ISnowflake snowflake, IPlanarProcessServiceMapInfoRepository PlanarProcessServiceMapRepository, ILogUtil logUtil, IMapper mapper)
        {
            _PlanarProcessServiceMapRepository = PlanarProcessServiceMapRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddPlanarProcessServiceMapAsync(PlanarProcessServiceMapInfoDto PlanarProcessServiceMapInfoDto)
        {
            var PlanarProcessServiceMapInfo = _Mapper.Map<PlanarProcessServiceMapInfoDto, PlanarProcessServiceMapInfo>(PlanarProcessServiceMapInfoDto);
            long id = _Snowflake.GetId();
            PlanarProcessServiceMapInfo.ID = id;
            bool result = await _PlanarProcessServiceMapRepository.InsertAsync(PlanarProcessServiceMapInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeletePlanarProcessServiceMapAsync(long ID)
        {
            bool result = await _PlanarProcessServiceMapRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdatePlanarProcessServiceMapAsync(PlanarProcessServiceMapInfoDto PlanarProcessServiceMapInfoDto)
        {
            var PlanarProcessServiceMapInfo = _Mapper.Map<PlanarProcessServiceMapInfoDto, PlanarProcessServiceMapInfo>(PlanarProcessServiceMapInfoDto);
            bool result = await _PlanarProcessServiceMapRepository.UpdateAsync(PlanarProcessServiceMapInfo);
            return result;
        }

        public async Task<List<PlanarProcessServiceMapInfoDto>> GetPlanarProcessServiceMapAsync(QueryPlanarProcessServiceMapInfoDto queryPlanarProcessServiceMapInfoDto)
        {
            var equipmentCode = queryPlanarProcessServiceMapInfoDto.EquipmentCode;
            var planarProcessServiceName = queryPlanarProcessServiceMapInfoDto.PlanarProcessServiceName;
            var exp = Expressionable.Create<PlanarProcessServiceMapInfo>();
            exp.AndIF(!string.IsNullOrEmpty(equipmentCode), (a) => a.EquipmentCode.ToString().Contains(equipmentCode));
            exp.AndIF(!string.IsNullOrEmpty(planarProcessServiceName), (a) => a.PlanarProcessServiceName.Contains(planarProcessServiceName) );
            var result = await _PlanarProcessServiceMapRepository.GetListAsync(exp.ToExpression());
            List<PlanarProcessServiceMapInfoDto> list = new List<PlanarProcessServiceMapInfoDto>();
            _Mapper.Map(result, list);
            return list;
        }

        public async Task<PlanarProcessServiceMapInfoDto> GetPlanarProcessServiceMapByIDAsync(long ID)
        {
            var result = await _PlanarProcessServiceMapRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<PlanarProcessServiceMapInfo, PlanarProcessServiceMapInfoDto>(result);
            return vModel;
        }
    }
}
